﻿/*
 * @Author: funlee
 * @Email: i@funlee.cn
 * @Date: 2018-10-29 21:55:23
 * @Description: 主入口 
 */

define(function(require) {
  /**
   *  引入第三方组件库
   */
  require('jquery')
  require('echarts')
  require('handlebars')
  /**
   *  引入模块组件
   */
  var oprate = require('./oprate');
  var map = require('./map');
  var amount = require('./amount');
  var leftBottom = require('./leftBottom');
  var rightBottom = require('./rightBottom');
  var rightTop = require('./rightTop');
  var foot = require('./foot');
  var leftTop = require('./leftTop');
  /**
   *  请求数据公用API组件
   */
  var util = require('util')
  var request = require('request')
  var baseConfig = require('baseConfig')
  /**
   *  mocke数据
   */
  var Index = {
    init: function() {
      var self = this;
      /**
       * 当缩放页面后，进行相应的缩放
       */
      window.addEventListener('resize', function(){
        util.zoom();
      })
      util.zoom();
      //二级节点和设备数量
      request.sendAjax(baseConfig.oprate, {}, function (data) {
        oprate.render(data);
      })
      // 地图
      request.sendAjax(baseConfig.map, {}, function (data) {
        window.mapData = data
        map.renderMap(data, true);
      })
      //顶部总数接口
      request.sendAjax(baseConfig.amount,{}, function (data) {
        amount.render(data);
      })
      // 攻击地址TOP10
      request.sendAjax(baseConfig.leftBottom,{}, function (data) {
        leftBottom.render(data)
      })
      // 实时处置率
      request.sendAjax(baseConfig.rightTop,{}, function (data) {
        rightTop.render(data)
      })
      // 告警地址TOP10
      request.sendAjax(baseConfig.rightBottom,{}, function (data) {
        rightBottom.render(data)
      })
      // 威胁信息
      request.sendAjax(baseConfig.foot,{}, function (data) {
        foot.render(data)
      })
      // 告警类型TOP5
      request.sendAjax(baseConfig.leftTop,{type: 'today'}, function (data) {
        leftTop.render(data)
      })
      this.bindEvent();
      this.getNowTime();
    },
    bindEvent() {
      $('.left-top .time-btn').find('span').on('click', function() {
        $(this).addClass('active').siblings().removeClass('active')
        var type = $(this).data('type');
        // 传参
        request.sendAjax(baseConfig.leftTop, {type}, function (data) {
          leftTop.render(data)
        })
      })

      $('.right-top .time-btn').find('span').on('click', function () {
        $(this).addClass('active').siblings().removeClass('active')
        var type = $(this).data('type');
        // 传参
        request.sendAjax(baseConfig.rightTop, {type},function (data) {
          rightTop.render(data)
        })
      })

      $('.scale-btn').on('click', function() {
        if($('body').hasClass('detail-page')) {
          $('body').removeClass('detail-page');
          map.renderMap(window.mapData, true)
        } else {
          $('body').addClass('detail-page');
          map.renderMap(window.mapData, false)
        }
      })

    },
    getNowTime() {
      setInterval(function () {
        var time = new Date();  
        var year = time.getFullYear(); 
        var month = time.getMonth() + 1;
        var day = time.getDate();

        var hour = time.getHours();
        var minutes = time.getMinutes();
        // var seconds = time.getSeconds();

        var t = year + '.' + month + '.' + day + ' ' + hour + ':' + minutes;

        $('.time').html(t)
      }, 1000);
    }

  }
  return Index;
})